package com.bestom.rideelectric.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.bestom.rideelectric.Entity.Constant;
import com.bestom.rideelectric.R;


public class WelcomeActivity extends BaseActivity {

    private final int REQUEST_CODE_CONTACT = 101;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_welcome);

        //发送广播给系统不计时
        sendBroadcastToSystemCancelTime();

    }

    @Override
    protected void onStart() {
        super.onStart();
        // 申请权限
        requestAllPower();
    }

    /**是否重新运行*/
    private boolean reRun = true;
    public void requestAllPower() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                new AlertDialog.Builder(this)
                        .setTitle("权限申请")
                        .setMessage("您已拒绝该权限，是否再次申请？")
                        .setPositiveButton("确定", (dialog, which) -> {
                            reRun = false;
                            ActivityCompat.requestPermissions(this,
                                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                            Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_CONTACT);
                        })
                        .setNegativeButton("取消", (dialog, which) -> finish())
                        .show();
            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_CONTACT);
            }
        }else toActivity();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_CODE_CONTACT){

            boolean flag = true;
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    flag = false;
                    break;
                }
            }

            if (flag) {
                toActivity();
            } else {
                // Permission Denied 权限被拒绝
                if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && reRun) {
                    //解释原因，并且引导用户至设置页手动授权
                    new AlertDialog.Builder(this)
                            .setTitle("权限申请")
                            .setMessage("您已拒绝该权限，是否再次申请？")
                            .setPositiveButton("去授权", (dialog, which) -> {
                                //引导用户至设置页手动授权
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getApplicationContext().getPackageName(), null);
                                intent.putExtra(":settings:show_fragment_as_subsetting", true);
                                intent.setData(uri);
                                startActivity(intent);
                            })
                            .setNegativeButton("取消", (dialog, which) -> finish())
                            .show();
                }else finish();
            }
        }
    }

    private void sendBroadcastToSystemCancelTime() {
        Intent intent1 = new Intent(Constant.Not_To_Monitor_User_Operation);
        sendBroadcast(intent1);
    }

    private void toActivity() {
        Handler h = new Handler();
        h.postDelayed(() -> {
            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }, 3000);
    }
}
